package hibari.server.service;

import hibari.PMF;
import hibari.client.model.Week;
import hibari.client.service.IWeekService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class WeekService extends RemoteServiceServlet implements IWeekService {
	private static final long serialVersionUID = 2998652697444655170L;

	@Override
	public Week getWeek(int weekNumber) {
		Week week = null;
		try {
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Query query = new Query("Week");
			query.addFilter("alive", FilterOperator.EQUAL, true);
			query.addFilter("weekNumber", FilterOperator.EQUAL, weekNumber);

			PreparedQuery result = ds.prepare(query);
			Iterator<Entity> iter = result.asIterator();
			if (!iter.hasNext()) {
				return null;
			}
			Entity entity = iter.next();
			week = PMF.get().getPersistenceManager().getObjectById(Week.class, entity.getKey());
		} catch (Exception e) {
			e.printStackTrace();
			week = null;
		}
		return week;
	}

	@Override
	public Week getLastWeek() {
		Week week = null;
		try {
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Query query = new Query("Week");
			query.addFilter("alive", FilterOperator.EQUAL, true);
			query.addSort("weekNumber", SortDirection.DESCENDING);

			PreparedQuery result = ds.prepare(query);

			Iterator<Entity> iter = result.asIterator();
			if (!iter.hasNext()) {
				return null;
			}

			Entity e = iter.next();
			week = PMF.get().getPersistenceManager().getObjectById(Week.class, e.getKey());
		} catch (Exception e) {
			e.printStackTrace();
			week = null;
		}
		return week;
	}

	@Override
	public List<Week> getAllWeeks() {
		ArrayList<Week> result = new ArrayList<Week>();
		Week week = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Query query = new Query("Week");
			query.addFilter("alive", FilterOperator.EQUAL, true);
			query.addSort("weekNumber", SortDirection.DESCENDING);

			PreparedQuery pq = ds.prepare(query);

			Iterator<Entity> iter = pq.asIterator();
			while (iter.hasNext()) {
				Entity e = iter.next();
				week = pm.getObjectById(Week.class, e.getKey());
				result.add(week);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Integer> getAllWeekNumbers() {
		ArrayList<Integer> result = new ArrayList<Integer>();
		try {
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Query query = new Query("Week");
			query.addFilter("alive", FilterOperator.EQUAL, true);
			query.addSort("weekNumber", SortDirection.DESCENDING);

			PreparedQuery pq = ds.prepare(query);

			Iterator<Entity> iter = pq.asIterator();
			while (iter.hasNext()) {
				Entity e = iter.next();
				long value = (Long) e.getProperty("weekNumber");
				result.add((int) value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Integer getLastWeekNumber() {
		Week week = null;
		try {
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			Query query = new Query("Week");
			query.addFilter("alive", FilterOperator.EQUAL, true);
			query.addSort("weekNumber", SortDirection.DESCENDING);

			PreparedQuery result = ds.prepare(query);

			Iterator<Entity> iter = result.asIterator();
			if (!iter.hasNext()) {
				return null;
			}

			Entity e = iter.next();
			week = PMF.get().getPersistenceManager().getObjectById(Week.class, e.getKey());
		} catch (Exception e) {
			e.printStackTrace();
			week = null;
		}
		return week == null ? 0 : week.getWeekNumber();
	}

}
